package com.example.thegreatwall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.thegreatwall.entity.ProductDetail;
import com.example.thegreatwall.mapper.ProductDetailMapper;
import com.example.thegreatwall.service.ProductDetailService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ProductDetailServiceImpl extends ServiceImpl<ProductDetailMapper, ProductDetail> implements ProductDetailService {

    @Override
    public Page<ProductDetail> getDetailsByProductId(Integer productId, Integer pageNum, Integer pageSize) {
        Page<ProductDetail> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<ProductDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductDetail::getProductId, productId)
                .eq(ProductDetail::getStatus, 1)
                .orderByAsc(ProductDetail::getPdPrice);
        return page(page, wrapper);
    }

    @Override
    public ProductDetail getDetail(Integer productDetailId) {
        return getById(productDetailId);
    }

    @Override
    public Page<ProductDetail> getAvailableDetails(Integer productId, String playtime, Integer pageNum, Integer pageSize) {
        Page<ProductDetail> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<ProductDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductDetail::getProductId, productId)
                .eq(ProductDetail::getStatus, 1)
                .gt(ProductDetail::getInventory, 0)
                .and(playtime != null, w -> w.isNull(ProductDetail::getPlaytime)
                        .or()
                        .eq(ProductDetail::getPlaytime, playtime))
                .orderByAsc(ProductDetail::getPdPrice);
        return page(page, wrapper);
    }

    @Override
    public Map<String, Object> timesss(Integer product_id) {
        return this.baseMapper.timesss(product_id);
    }

    @Override
    public List<ProductDetail> getProductDetail(Integer detail_id, Integer user_id) {
        return this.baseMapper.getProductDetail(detail_id, user_id);
    }
}
